//
//  RightMVTableViewDelegateSource.m
//  RongRTCMeeting
//
//  Created by LiuLinhong on 2017/08/21.
//  Copyright © 2017年 Beijing Rongcloud Network Technology Co. , Ltd. All rights reserved.
//

#import "RightMVTableViewDelegateSource.h"
#import "MeetingViewController.h"
#import "RightMVTableViewCellView.h"

@interface RightMVTableViewDelegateSource()

@property (nonatomic, strong) MeetingViewController *meetingViewController;

@end

@implementation RightMVTableViewDelegateSource

- (instancetype)initWithViewController:(NSViewController *)vc
{
    self = [super init];
    if (self)
    {
        self.meetingViewController = (MeetingViewController *) vc;
        if (!self.itemDic)
            self.itemDic = [NSMutableDictionary dictionary];
    }
    return self;
}

#pragma mark - NSTableViewDataSource
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
    return [LarkMeetingManager.remoteAllMemberArray count];
}

#pragma mark - NSTableViewDelegate
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    MeetingVideoViewModel *model = (MeetingVideoViewModel *)LarkMeetingManager.remoteAllMemberArray[row];
//    NSDictionary *tmpDic = @{kUserID:model.userID, kUserName:model.userName, kUserType:@(model.userType), kAudioVideoType:@(model.avType), kHeaderImage:model.headIconView};
    
    NSMutableDictionary *tmpDic = [NSMutableDictionary dictionary];
    if (model.userID)
        tmpDic[kUserID] = model.userID;
    if (model.userName)
        tmpDic[kUserName] = model.userName;
    if (model.userType)
        tmpDic[kUserType] = @(model.userType);
    if (model.avType)
        tmpDic[kAudioVideoType] = @(model.avType);
    if (model.headIconView)
        tmpDic[kHeaderImage] = model.headIconView;
    
    RightMVTableViewCellView *cell = (RightMVTableViewCellView *)self.itemDic[tmpDic[kUserID]];
    
    if (!cell)
    {
        cell = [[RightMVTableViewCellView alloc] init];
        cell.meetingViewController = self.meetingViewController;
        [self.itemDic setObject:cell forKey:tmpDic[kUserID]];
    }
    
    cell.isHost = (LarkMeetingManager.observerIndex == RongRTC_User_Host) ? YES : NO;
    [cell.headImageView addSubview:model.headIconView];
    switch (model.userType)
    {
        case RongRTC_User_Normal:
        {
            cell.isUpgrade = NO;
            [cell resignHost];
            [cell mouseExited:[NSEvent new]];
        }
            break;
        case RongRTC_User_Observer:
        {
            cell.isUpgrade = YES;
            [cell resignHost];
        }
            break;
        case RongRTC_User_Host:
        {
            cell.isUpgrade = NO;
            [cell becomeHost];
            [cell mouseExited:[NSEvent new]];
        }
            break;
        default:
            break;
    }
    
    switch (model.avType)
    {
        case RongRTC_User_Only_Audio:
        {
            cell.isOpenMic = YES;
            cell.isOpenCamera = NO;
        }
            break;
        case RongRTC_User_Audio_Video:
        {
            cell.isOpenMic = YES;
            cell.isOpenCamera = YES;
        }
            break;
        case RongRTC_User_Only_Video:
        {
            cell.isOpenMic = NO;
            cell.isOpenCamera = YES;
        }
            break;
        case RongRTC_User_Audio_Video_None:
        {
            cell.isOpenMic = NO;
            cell.isOpenCamera = NO;
        }
            break;
        default:
            break;
    }
    
    cell.dataDic = tmpDic;
    if (tmpDic[kUserName])
        cell.userNameTextField.stringValue = tmpDic[kUserName];
    
    return cell;
}

- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
{
    return 44;
}

- (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row
{
    return NO;
}

@end
